package com.apestech.framework.dictionary.modul;

import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.util.StringUtil;

import java.util.ArrayList;

/**
 * @author hup
 * @create 2018-08-24-11:06
 */
public class ImportedKey extends StringUtil {

    public class ImportedField{
        private String columnName;
        private String pkTableName;
        private String pkColumnName;
        private String fkeyName;

        public ImportedField(JSONObject map){
            if(map==null) return;
            columnName=map.getString("columnName");
            pkTableName=map.getString("pkTableName");
            pkColumnName=map.getString("pkColumnName");
            fkeyName=map.getString("fkeyName");
        }

        public ImportedField(String columnName, String pkTableName, String pkColumnName, String fkeyName){
            this.columnName = columnName;
            this.pkTableName = pkTableName;
            this.pkColumnName = pkColumnName;
            this.fkeyName= fkeyName;
        }

        public String getPkTableName() {
            return pkTableName;
        }

        public String getPkColumnName() {
            return pkColumnName;
        }

        public String getColumnName() {
            return columnName;
        }

        public String getFkeyName() {
            return fkeyName;
        }
    }

    private JSONObject field=new JSONObject();
    private JSONObject table=new JSONObject();
    private String pkTable="";
    private String fields="";
    private String[] pkTableList;
    private String[] fieldList;

    public ImportedKey(){ }

    public ImportedKey(JSONObject map) throws Exception {
        pkTable=map.getString("pkTable");
        fields=map.getString("fields");
        pkTableList= castToAString((ArrayList<String>) map.get("pkTableList"));
        fieldList= castToAString((ArrayList<String>) map.get("fieldList"));
        JSONObject t= JSON.toJSON(map.get("field"));
        if(t!=null){
            for(String key:t.getKeylist()){
                ImportedField f=new ImportedField(JSON.toJSON(t.get(key)));
                field.put(key, f);
                if(table.get(f.pkTableName)==null) table.put(f.pkTableName, key);
            }
        }
    }

    public void add(String key, String columnName, String pkTableName, String pkColumnName, String fkeyName){
        ImportedField f=new ImportedField(columnName, pkTableName, pkColumnName, fkeyName);
        field.put(key, f);
        if(table.get(pkTableName)==null) table.put(pkTableName, key);
        if((","+pkTable+",").indexOf(","+pkTableName+",")<0){
            if(!"".equals(pkTable)) pkTable=pkTable+",";
            pkTable=pkTable+pkTableName;
        }
        if(!"".equals(columnName)) fields=fields+",";
        fields=fields+columnName;

    }

    public ImportedField getFieldByTable(String tabid){
        String key=table.getString(tabid);
        if(key==null) return null;
        return get(key);
    }

    public ImportedField get(String key){
        Object k=field.get(key);
        if(k==null) return null;
        return (ImportedField) k;
    }

    public String getPkTableName(String key){
        ImportedField f=get(key);
        if(f==null) return null;
        return f.getPkTableName();
    }

    public String getColumnName(String key){
        ImportedField f=get(key);
        if(f==null) return null;
        return f.getColumnName();
    }

    public String getPkColumnName(String key){
        ImportedField f=get(key);
        if(f==null) return null;
        return f.getPkColumnName();
    }

    public JSONObject getField() {
        return field;
    }

    public void setField(JSONObject field) {
        this.field = field;
    }

    public int size(){
        return field.size();
    }

    public String[] getPkTableList() {
        if(pkTableList==null){
            pkTableList=pkTable.split(",");
        }
        return pkTableList;
    }

    public String[] getFieldList() {
        if(fieldList==null){
            fieldList=fields.split(",");
        }
        return fieldList;
    }
}
